/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import databaseHandling.DatabaseHandler;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import utility.PageHandler;
import utility.Resizeable;
import view.Kasse.KassePanel;
import view.Kasse.StartPanel;

/**
 *
 * @author Jeppe
 */
public final class MainGUI extends javax.swing.JFrame {

    private final PageHandler ph;
    private KassePanel kassePanel;
    private StartPanel startPanel;
    private final Resizeable resizeable;
    private final SkærmNumpad skærmNumpad;
    private final JFrame[] jFrameList;
    private boolean startingUp;

    /**
     * Creates new form MainGUI
     */
    public MainGUI() {
        initComponents();
        startingUp = true;
        try {
            DatabaseHandler.getInstance().openConn(null);
        } catch (SQLException ex) {
            Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        ph = new PageHandler((CardLayout) CardLayoutPanel.getLayout(), CardLayoutPanel);
        setSize(new Dimension(904, 790));
        skærmNumpad = SkærmNumpad.getInstance();
        jFrameList = new JFrame[1];
        jFrameList[0] = skærmNumpad;
        createPanels();
        ph.show(kassePanel.getClass().getName());
        skærmNumpad.setLocation(getX() + kassePanel.getSkærmNumpadPlacering().x + 10, getY() + kassePanel.getSkærmNumpadPlacering().y + 90 + 23);
        resizeable = new Resizeable(this, getBounds());
        startingUp = false;
        repaint();
        revalidate();

    }

    public Resizeable getResizeable() {
        return resizeable;
    }

    private void createPanels() {
        kassePanel = new KassePanel(this);
        startPanel = new StartPanel();
        ph.addPage(startPanel, startPanel.getClass().getName());
        ph.addPage(kassePanel, kassePanel.getClass().getName());
        revalidate();
        repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        CardLayoutPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(101, 235, 250));
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
                formWindowLostFocus(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                formComponentMoved(evt);
            }
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });
        getContentPane().setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("Jpanel1"); // NOI18N
        jPanel1.setOpaque(false);
        jPanel1.setLayout(null);

        jLabel3.setFont(new java.awt.Font("IrisUPC", 1, 100)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Nem-IS");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(280, 0, 290, 80);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 902, 80);
        jPanel1.getAccessibleContext().setAccessibleName("HEJ");

        CardLayoutPanel.setOpaque(false);
        CardLayoutPanel.setLayout(new java.awt.CardLayout());
        getContentPane().add(CardLayoutPanel);
        CardLayoutPanel.setBounds(0, 80, 905, 680);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/div/Nemisbaggrund.png"))); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(0, 80, 800, 650);

        jLabel2.setBackground(new java.awt.Color(226, 240, 255));
        jLabel2.setOpaque(true);
        getContentPane().add(jLabel2);
        jLabel2.setBounds(0, -6, 910, 760);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        if (!startingUp) {
            resizeable.resize(jFrameList);
//            System.out.println("Jpanel4 = "+kassePanel.getSkærmNumpadPlacering().x + ", " +kassePanel.getSkærmNumpadPlacering().y);
            skærmNumpad.setBounds(getX() + kassePanel.getSkærmNumpadPlacering().x + 10, getY() + kassePanel.getSkærmNumpadPlacering().y + 90 + 23, kassePanel.getSkærmNumpadPlacering().width - 5, kassePanel.getSkærmNumpadPlacering().height - 5);
        }
    }//GEN-LAST:event_formComponentResized

    private void formComponentMoved(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentMoved
        if (!startingUp) {
            resizeable.resize(jFrameList);
            skærmNumpad.setBounds(getX() + kassePanel.getSkærmNumpadPlacering().x + 10, getY() + kassePanel.getSkærmNumpadPlacering().y + 90 + 23, kassePanel.getSkærmNumpadPlacering().width - 5, kassePanel.getSkærmNumpadPlacering().height - 5);
        }
    }//GEN-LAST:event_formComponentMoved

    private void formWindowLostFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowLostFocus
        skærmNumpad.setVisible(false);
    }//GEN-LAST:event_formWindowLostFocus

    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
        if (ph.getCurrentPage().equals(kassePanel.getClass().getName())) {
            skærmNumpad.setVisible(true);
        }
    }//GEN-LAST:event_formWindowGainedFocus

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel CardLayoutPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

}
